package io.sunny.platform.backend.utils;

import org.bouncycastle.crypto.engines.SM4Engine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Author SUNNY
 * @Date 2025/7/17 14:28
 * @PackageName: io.sunny.platform.security.utils
 * @Description: SM4 加解密工具类（国密算法）
 * @Version 1.0
 */
public class Sm4Utils {
    /**
     * 16字节国密Key
     */
    private static final String DEFAULT_KEY = "1234567890abcdef";

    public static String encrypt(String plainText) throws Exception {
        return encrypt(plainText, DEFAULT_KEY);
    }

    public static String decrypt(String ECBBlockCipher) throws Exception {
        return decrypt(ECBBlockCipher, DEFAULT_KEY);
    }

    public static String encrypt(String plainText, String key) throws Exception {
        PaddedBufferedBlockCipher cipher = createCipher(true, key.getBytes(StandardCharsets.UTF_8));
        byte[] input = plainText.getBytes(StandardCharsets.UTF_8);
        byte[] output = new byte[cipher.getOutputSize(input.length)];
        int length = cipher.processBytes(input, 0, input.length, output, 0);
        length += cipher.doFinal(output, length);
        byte[] encrypted = new byte[length];
        System.arraycopy(output, 0, encrypted, 0, length);
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String base64Cipher, String key) throws Exception {
        byte[] input = Base64.getDecoder().decode(base64Cipher);
        PaddedBufferedBlockCipher cipher = createCipher(false, key.getBytes(StandardCharsets.UTF_8));
        byte[] output = new byte[cipher.getOutputSize(input.length)];
        int length = cipher.processBytes(input, 0, input.length, output, 0);
        length += cipher.doFinal(output, length);
        byte[] decrypted = new byte[length];
        System.arraycopy(output, 0, decrypted, 0, length);
        return new String(decrypted, StandardCharsets.UTF_8);
    }

    private static PaddedBufferedBlockCipher createCipher(boolean forEncryption, byte[] key) {
        SM4Engine engine = new SM4Engine();
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
                new CBCBlockCipher(engine), new PKCS7Padding());
        cipher.init(forEncryption, new KeyParameter(key));
        return cipher;
    }

    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://localhost:3306/study2";
        String encryptedUrl = encrypt(url);
        System.out.println("Encrypted URL: " + encryptedUrl);
        System.out.println("Decrypted: " + decrypt(encryptedUrl));
        String user = "root";
        System.out.println("Encrypted user: " + encrypt(user));
        String host = "192.168.100.148";
        String port = "6879";
        String pwd = "123456";
        String database = "10";
        System.out.println("Encrypted host: " + encrypt(host));
        System.out.println("Encrypted port: " + encrypt(port));
        System.out.println("Encrypted pwd: " + encrypt(pwd));
        System.out.println("Encrypted database: " + encrypt(database));
    }
}
